﻿/*
创建人：longb
日期：2022-05-21
描述：任务单管理js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSTaskOrderManagementHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        page.initCombox();
        customizedEasyuiPagination('pp'); //定制化分页
        page.GetTaskOrderData();
    },
    //页面事件绑定
    initCombox:function(){
     $('#OrderType').combobox({
            data: [{ id: 1, text: '正常' }, { id: 0, text: '临时'}],
            valueField: 'id',
            textField: 'text',
            value:'1'
        });
        $('#Status').combobox({
            data: [{ id: '未生成', text: '未生成' }, { id: '已生成', text: '已生成'},{id:'全部',text:'全部'}],
            valueField: 'id',
            textField: 'text',
            value:'未生成'
        });
         $('#BigSmall').combobox({
            data: [{ id: '无', text: '无' }, { id: '有', text: '有'}],
            valueField: 'id',
            textField: 'text',
            value:'有'
        });
    },
    bind: function () {
        //任务单信息
        $("#gdContainerInfo").datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            onSelect: function (index, row) { $("#gdContainerInfo").datagrid("uncheckAll").datagrid("checkRow",index);},//单选行，去掉同时勾选
            onCheck: function (index, rowData) { page.GetWorkFlowData();page.GetBigSmallData("");  },
            onUncheck: function (index, rowData) { page.GetWorkFlowData();page.GetBigSmallData("");   },
            onCheckAll: function (index, rowData) { $("#NoticeBillTable").datagrid("clearSelections"); },
            //onUncheckAll: function (index, rowData) { page.GetWorkFlowData();  },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
		         { field: 'TOGETHERFLAG', title: '任务单号', width: 180 },
		         { field: 'WORKFLOWTYPE', title: '任务类型', width: 60,align: 'center' }, //--
                 {field: 'PRODUCTNAME', title: '件号', width: 120 },
                 { field: 'DESCRIPTION', title: '件号名称', width: 150 },
                 { field: 'WFDESC', title: '工艺名称', width: 150 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始时间', width: 130},
                 { field: 'PLANNEDFINISHDATE', title: '计划完成时间', width: 130},
                 { field: 'TASKBILLSTATUS', title: '任务单状态', width: 70,align: 'center' },
                 { field: 'WORKFLOWSTEPNAME', title: '阶段', width: 150 }
//                 { field: 'QTL', title: '预齐套率', width: 90, align: 'center', formatter: function (value, row, index) { if (value == 1) { return '<span style="color:#0000ff">' + value * 100 + '%</span>'; } else { return '<span style="color:red">' + Number(value).toFixed(2) * 100 + '%</span>'; } } }
	           ]]
        });
        //工序信息
        $('#SpecTable').datagrid({
            fitColumns: true,
            singleSelect: true,
            columns: [[
		            { field: 'SPECNO', title: '工序号', width: 50, align: 'center' },
		            { field: 'OPDESC', title: '工序名称', width: 150 }
                ]],
            onSelect: function (rowIndex, rowData) {
                $('#BigSmallTable').datagrid('loadData', [])
                page.GetBigSmallData(rowData.SPECID);
            }
        });
        //大件信息
        $('#BigTable').datagrid({
            columns: [[
		            { field: 'PRODUCTNAME', title: '件号', width: 100 },
		            { field: 'DESCRIPTION', title: '件号名称', width: 150 },
		            { field: 'FIRSTASSEMBLEQTY', title: '单台数量', width: 90, align: 'center' },
                    { field: 'SumQty', title: '实际数量', width: 90, align: 'center' },
                    { field: 'PRODUCTBATCHNO', title: '批次号', width: 150, align: 'center' },
                    { field: 'QTL', title: '齐套率', width: 90, align: 'center', formatter: function (value, row, index) { var c =row.SumQty==null?0: row.SumQty / row.FIRSTASSEMBLEQTY; if (c == 1) { return '<span style="color:#0000ff">' + c * 100 + '%</span>'; } else { return '<span style="color:red">' + c * 100 + '%</span>'; } } },
                    { field: 'SPECNAME', title: '工序', width: 250}
                ]]
        });
        //小件信息
         $('#SmallTable').datagrid({
            columns: [[
		            { field: 'PRODUCTNAME', title: '件号', width: 100 },
		            { field: 'DESCRIPTION', title: '件号名称', width: 150 },
		            { field: 'FIRSTASSEMBLEQTY', title: '单台数量', width: 90, align: 'center' },
                    { field: 'SumQty', title: '实际数量', width: 90, align: 'center' },
                    { field: 'PRODUCTBATCHNO', title: '批次号', width: 150, align: 'center' },
                    { field: 'QTL', title: '齐套率', width: 90, align: 'center', formatter: function (value, row, index) { var c =row.SumQty==null?0: row.SumQty / row.FIRSTASSEMBLEQTY; if (c == 1) { return '<span style="color:#0000ff">' + c * 100 + '%</span>'; } else { return '<span style="color:red">' + c * 100 + '%</span>'; } } },
                    { field: 'SPECNAME', title: '工序', width: 250}
                ]],
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetTaskOrderData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetTaskOrderData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        $('#MaterialTable').datagrid('loadData', []);
        page.GetTaskOrderData();
    },
    //查询任务单信息
    GetTaskOrderData: function () {
        $('#MaterialTable').datagrid('loadData', [])
        $('#SpecTable').datagrid('loadData', []);
        $('#BigTable').datagrid('loadData', []);
        $('#SmallTable').datagrid('loadData', []);
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetTaskOrderData",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify(data)
        };
        page.post(param, function (res) {
            if (res.Result) {
                page.pagetotal = res.RowCount;
                $('#gdContainerInfo').datagrid('loadData', res.DBTable)
            }
        });
    },
    //查询工序信息
    GetWorkFlowData: function () {
        $('#SpecTable').datagrid('loadData', []);
        var CheckedRows = $('#gdContainerInfo').datagrid('getChecked');
        var WorkFlowId = ""
        if (CheckedRows.length > 0) {
            WorkFlowId = CheckedRows[0].WORKFLOWID;
        }
        if (WorkFlowId == "") { return; }
        var param = {
            action: "GetWorkFlowData",
            workflowid: WorkFlowId
        };
        page.post(param, function (res) {
            if (res.Result) {
                $('#SpecTable').datagrid('loadData', res.DBTable)
            }
        });
    },
    //大小件信息
    GetBigSmallData: function (workflowstepid) {
       $('#BigTable').datagrid('loadData', []);
       $('#SmallTable').datagrid('loadData', []);
       var CheckedRows = $('#gdContainerInfo').datagrid('getChecked');
       var param = {
            action: "GetBigSmallData",
            workflowstepid: workflowstepid,
            data: JSON.stringify(CheckedRows[0]),
        };
        page.post(param, function (res) {
            if (res.Result) {
                $('#BigTable').datagrid('loadData', res.DBTable);
                $('#SmallTable').datagrid('loadData', res.ReturnData);
            }
        });
    },
    CreateTaskOrder:function(){
        clearMessage();
        var Rows = $('#gdContainerInfo').datagrid('getChecked');
        if(Rows.length==0){displayMessage("请勾选至少一条任务单再进行操作", false);return;}
         var param = {
            action: "CreateTaskOrder",
            HasBom:$('#BigSmall').combobox('getValue'),
            data: JSON.stringify(Rows),
        };
        page.post(param, function (res) {
            if(res.Result){page.GetTaskOrderData();}
           displayMessage(res.Message, res.Result);
        });
    },
    IssueERPMaterialPopupForm:function(){
        var obj={title:'调整',href:'IssueERPMaterialPopupForm.aspx',width:'1040',height:'650'};
        var Row = $('#gdContainerInfo').datagrid('getSelected');
        if(Row==null){displayMessage("请选择一条任务单再进行操作", false);return;}
        if($('#BigSmall').combobox('getValue')=="无"){displayMessage("无大小件目录不需调整", false);return;}
         page.post({ action: "IssueERPMaterialPopupForm", data: JSON.stringify(Row) }, function (res) {
        if (res.Result) { page.showModalDialog(obj); }
      });
    },
    IssueMaterialAdjustPopupForm:function(){
        var obj={title:'',href:'IssueMaterialAdjustPopupForm.aspx',width:'850',height:'650'};
        var Rows = $('#gdContainerInfo').datagrid('getChecked');
        if(Rows.length==0){displayMessage("请勾选至少一条任务单再进行操作", false);return;}
        var IdList=[];
        $.each(Rows,function(index,item){
            if(item.CONTAINERID!=""){
                IdList.push(item.CONTAINERID);
            }
        });
        if(IdList.length==0){displayMessage("任务单没有生成", false);return;}
         page.post({ action: "IssueMaterialAdjustPopupForm", data:IdList.join(',')  }, function (res) {
        if (res.Result) { page.showModalDialog(obj); }
      });
    },
    PrintData:function(t,type){
        if($('#BigSmall').combobox('getValue')=="无"){displayMessage("无大小件目录不需打印", false);return;}
        var obj={title:'',href:'TaskBillManagePrint.aspx?_Type='+type,width:'850',height:'650'};
        var Rows = $('#gdContainerInfo').datagrid('getChecked');
        if(t=="all"||t=="allbig"||t=="allsmall"){
            if(Rows.length!=1){displayMessage("请勾选一条任务单再进行操作", false);return;}
        }
        page.post({ action: "PrintData",type:t, data:JSON.stringify(Rows)  }, function (res) {
        if (res.Result) { window.open(obj.href,'_blank'); }
      });
    },
    showModalDialog: function (obj) {
        clearMessage();
      var someValue = window.showModalDialog(obj.href, "", "dialogWidth:" + obj.width + "px; dialogHeight:" + obj.height + "px; status=no; center: Yes; resizable: NO;");
      if (obj.title != undefined && obj.title == "调整") {
        page.GetBigSmallData("");
      }
    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.initCombox();
    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});